var AjaxCrud = QualityCheck.AjaxCRUD;
var serverUrl = QualityCheck.GPUD.ServiceConfig;
$(function(){
	var usercode = $("#user-name",parent.parent.document).attr("code");
	var userkey = $("#user-name",parent.parent.document).attr("key");
	var usertype = $("#user-name",parent.parent.document).attr("usertype");
	keyCheck(usercode,usertype,userkey,function(){
		initSmPages();
		window.onresize = function () {
			initSmPages();
		};
		pagebtns();
	});
});
function pagebtns(){
	$("#subject-save-btns").click(function(){
		if(checkSubject() && check2("check-fail")){
			addSubject();
		}
	});	
	$("#subject-subjectcode,#subject-coursecontent").unbind("keyup");
	$("#subject-subjectcode,#subject-coursecontent").keyup(function(){
		this.value=this.value.replace(/^ +| +$/g,'');
	});
	check("subject-subjectcode",isNull,"null");
	check("subject-coursecontent",isNull,"null");
	
}
function addSubject(){
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
	var params = {};
	var param = {};
	params["subjectcode"] = encodeURI($("#subject-subjectcode").val());			
	params["coursecontent"] = encodeURI($("#subject-coursecontent").val());
	params["type"] = $("#subject-type option:selected").val();
	params["schoolcode"] = schoolcode;
	for(var key in params){
		if(params[key] != ""){
			param[key] = params[key];
		}
	}
	var data = {
			cmd:"addCourseSubjectCode",
			model:"course",
			userKey:userKey,
			data:param,
			remark:"",						
	};
	processBegin("正在保存中···");
	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			processEnd("添加科目成功");
		}else{
			processError("科目编码重复，添加失败");
		}
	},null,null);
}
function checkSubject(){
	if($("#subject-subjectcode").val() == "" ){
		$("#subject-subjectcode-ex").html("不能为空");
		return false; 
	}else if($("#subject-coursecontent").val() == ""){
		$("#subject-coursecontent-ex").html("不能为空");
		return false; 
	}else{
		return true;
	}
}
function initSmPages(){
	var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
	$(".right-content").height(rcH);
	
	var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
	$(".content-data-show").height(cdsH);
}